home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
vbdatabs
/
hexdump.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1999-03-17
|
18KB
|
646 lines
// ------------------------------- //
// -------- Start of File -------- //
// ------------------------------- //
// ----------------------------------------------------------- //
// C++ Source Code File Name: hexdump.cpp
// Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
// Produced By: Doug Gaer
// File Creation Date: 02/13/1996
// Date Last Modified: 03/17/1999
// ----------------------------------------------------------- //
// ------------- Program description and details ------------- //
// ----------------------------------------------------------- //
/*
THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
THE ENTIRE RISK OF THE QUALITY AND PERFORMANCE OF THIS SOFTWARE
IS WITH YOU. SHOULD ANY ELEMENT OF THIS SOFTWARE PROVE DEFECTIVE,
YOU WILL ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR
CORRECTION.
This program is used to display the contents of a specified
file, in both hexadecimal and ASCII, to the console.
*/
// ----------------------------------------------------------- //
#include <iostream.h>
#include <fstream.h>
#include <ctype.h>
#include <iomanip.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hxcrc.h"
// Define this macro for DOS file opens (the default is UNIX)
// #ifndef __DOS__
// #define __DOS__
// #endif
// Hexdump version number and program name
const double HexdumpVersionNumber = 1031.101;
const char *ProgramName = "hexdump";
// Program constants
const int MAX_LINE = 255; // Maximum characters per line
const int hxBuffSize = 16; // Maximum number of bytes per line
const int decBuffSize = 8; // Maximum number of bytes per line
// Constants for text parser
const int MAXWORDLENGTH = 255;
const int MAXWORDS = 255;
// Program globals
char in_file[MAX_LINE]; // Input file
char out_file[MAX_LINE]; // Optional output file name
int prompting = 0; // Enable prompting during console dump
int use_console = 1; // Dump to console if no output file is specified
int decimal_mode = 0; // Display in decimal mode (Default is HEX)
int building_bin_file = 0; // True if building binary file from text file
int strip_comments = 0; // True if not adding comments
int display_crc32 = 0; // True if displaying 32-bit crc checksum
int display_crc16 = 0; // True if displaying 16-bit crc checksum
int NumLines = 8; // Default maximum number of lines to display
// Table for HEX to binary conversions
const char *bintab[16] = {
"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"
};
// Program Functions
void ProcessArgs(int argc, char *argv[]);
void HelpMessage(const char *program_name, const double version_number);
int HexFileDump(fstream &infile, ostream &stream);
int DecFileDump(fstream &infile, ostream &stream);
void BuildBinaryFile(char *ifname, char *ofname);
void TruncateLine(char *line, char tline[MAX_LINE], char delimiter1 = '\t',
char delimiter2 = ';');
int text_parse(char *string, char words[MAXWORDS][MAXWORDLENGTH],
int*numwords, char sepchar1 = ' ', char sepchar2 = ' ');
void HelpMessage(const char *program_name, const double version_number)
{
char vbuffer[255];
sprintf(vbuffer, "%.3f", version_number);
cout << endl;
cout << "Hexadecimal file dump program version "
<< vbuffer << endl;
cout << "Usage: " << program_name << " [switches] infile "
<< "outfile (optional)" << endl;
cout << "Switches: -? = Display this help message." << endl;
cout << " -c = Display a 32-bit CRC checksum value." << endl;
cout << " -C = Display a 16-bit CRC checksum value." << endl;
cout << " -d = Display in decimal mode (Default is HEX)."
<< endl;
cout << " -p# = Prompting after displaying # line: -p10"
<< endl;
cout << " -r = Rebuild binary file from hex dump text file.\n"
<< " Only works for files created in hex mode."
<< endl;
cout << " -s = Strip comments from output." << endl;
cout << endl;
cout << " -x = Convert 32-bit HEX number to DEC: -Xfefe"
<< endl;
cout << " -X = Convert signed 32-bit HEX number to DEC."
<< endl;
cout << " -B = Convert 32-bit HEX number to binary: -Bfefe"
<< endl;
cout << " -D = Convert DEC number to HEX: -D23" << endl;
cout << endl;
exit(0);
}
void ProcessArgs(int argc, char *argv[])
// Process the program's argument list
{
int i, ibuf;
char *sbuf;
unsigned long ulbuf, bbuf;
long lbuf;
for(i = 1; i < argc; i++ ) {
if(*argv[i] == '-') {
char sw = *(argv[i] +1);
switch(sw) {
case '?' :
HelpMessage(ProgramName, HexdumpVersionNumber);
break;
case 'c':
display_crc32 = 1;
break;
case 'C':
display_crc16 = 1;
break;
case 'p' :
prompting = 1;
ibuf = atoi(&argv[i][2]);
if(ibuf > 0) NumLines = ibuf;
break;
case 'd' :
decimal_mode = 1;
break;
case 'r' :
building_bin_file = 1;
break;
case 's' :
strip_comments = 1;
break;
case 'x' : {
sbuf = &argv[i][2];
cout << endl;
sscanf(sbuf, "%x", &ulbuf);
cout.setf(ios::uppercase);
cout << "0x" << hex << ulbuf << " = " << dec << ulbuf << endl;
cout << endl;
exit(0);
break;
}
case 'X' : {
sbuf = &argv[i][2];
cout << endl;
sscanf(sbuf, "%x", &lbuf);
cout.setf(ios::uppercase);
cout << "0x" << hex << lbuf << " = " << dec << lbuf << endl;
cout << endl;
exit(0);
break;
}
case 'D' : {
sbuf = &argv[i][2];
cout << endl;
sscanf(sbuf, "%u", &ulbuf);
cout.setf(ios::uppercase);
cout << ulbuf << " = " << "0x" << hex << ulbuf << endl;
cout << endl;
exit(0);
break;
}
case 'B' : {
sbuf = &argv[i][2];
cout << endl;
sscanf(sbuf, "%x", &ulbuf);
cout.setf(ios::uppercase);
cout << "0x" << hex << ulbuf << " = ";
bbuf = (ulbuf & 0xf0000000)>>28;
cout << bintab[bbuf] << ' ';
bbuf = (ulbuf & 0xf000000)>>24;
cout << bintab[bbuf] << ' ';
bbuf = (ulbuf & 0xf00000)>>20;
cout << bintab[bbuf] << ' ';
bbuf = (ulbuf & 0xf0000)>>16;
cout << bintab[bbuf] << ' ';
bbuf = (ulbuf & 0xf000)>>12;
cout << bintab[bbuf] << ' ';
bbuf = (ulbuf & 0xf00)>>8;
cout << bintab[bbuf] << ' ';
bbuf = (ulbuf & 0xf0)>>4;
cout << bintab[bbuf] << ' ';
bbuf = ulbuf & 0x0f;
cout << bintab[bbuf] << endl;
cout << endl;
exit(0);
break;
}
default:
cerr << endl;
cerr << "Unknown switch " << argv[i] << endl;
cerr << "Exiting..." << endl;
cerr << endl;
exit(0);
break;
}
}
else {
strcpy(in_file, argv[i]); // Input file name
if(argv[i+1]) {
strcpy(out_file, argv[i+1]); // Output file name
break;
}
else
break;
}
}
}
int HexFileDump(fstream &infile, ostream &stream)
{
register int i, j;
int count = 0;
char c[hxBuffSize];
// Print all text in upper case characters
stream.setf(ios::uppercase);
while(!infile.eof()) {
for(i = 0; i < hxBuffSize && !infile.eof(); i++) {
infile.get(c[i]);
}
if(i < hxBuffSize) i--; // Get rid of eof;
for(j = 0; j < i; j++) {
long xx = (long)c[j];
// Set fill chars and reset first 24 bits for one byte display
stream << setfill('0') << setw(2) << hex << (xx & 0xFF);
stream << " "; // Print one space between the bytes
}
// Set the line spacing to a minimum of bytes per line
for(; j < hxBuffSize; j++) stream << " "; // Insert 3 spaces
if(!strip_comments) {
// Separate hex output by one tab and denote comments
// with a semicolon.
stream << "\t" << "; ";
// Filter out any non-printable characters
for(j = 0; j < i; j++)
if(isgraph(c[j])) stream << c[j];